package com.hanahina.CourseDesign;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {

	JFrame jFrame;
	JFrame jStartFrame;
	JFrame jHelpFrame;
	Random random=new Random();
	Color[] colors={Color.RED,Color.GREEN,Color.BLUE,
			Color.YELLOW,Color.PINK,Color.ORANGE};
	String[] strings={"A","B","C","D","E","F","G",
			"H","I","J","K","L","M","N",
			"O","P","Q","R","S","T","U",
			"V","W","X","Y","Z"};
	boolean b=true;
	int score=0;
	int speed=10;
	int c=random.nextInt(colors.length);
	int k=random.nextInt(26);			//nextInt获取整数型随机数
	int x=random.nextInt(300);
	
	public static void main(String[] args) {
		new Main().show();
	}
	
	public void show() {
		//======创建开始画面======
		jStartFrame=new JFrame("打字游戏");
		jStartFrame.setBounds(500, 250, 500, 500);
		ImageIcon icon1=new ImageIcon("tql.jpg");
		JLabel jLabel1=new JLabel(icon1);
		jLabel1.setSize(icon1.getIconWidth(), icon1.getIconHeight());
		jStartFrame.getLayeredPane().add(jLabel1,new Integer(Integer.MIN_VALUE));
		JPanel jPanel1=(JPanel)jStartFrame.getContentPane();
		jPanel1.setOpaque(false);
		jPanel1.setLayout(new FlowLayout());
		jStartFrame.setLayout(null);
		
			JLabel jWordLabel=new JLabel();
			jWordLabel.setBounds(190, 0, 150, 150);
			jWordLabel.setFont(new Font("黑体",Font.BOLD,32));
			jWordLabel.setForeground(Color.WHITE);
			jWordLabel.setText("打字游戏");
			jStartFrame.add(jWordLabel);
		  
			JButton jButton=new JButton("进入游戏");
			jButton.setBounds(200,100,100,50);
			jButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					jFrame.setVisible(true);
					jStartFrame.setVisible(false);
				}
			});
			jStartFrame.add(jButton);
		  
			JButton jButton1=new JButton("帮助");
			jButton1.setBounds(200,200,100,50);
			jStartFrame.add(jButton1);
			jButton1.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					jHelpFrame.setVisible(true);
				}
			});
		  
			JButton jButton2=new JButton("退出");
			jButton2.setBounds(200,300,100,50);
			jButton2.addActionListener(new ActionListener() {
			
				@Override
				public void actionPerformed(ActionEvent e) {
					jStartFrame.dispose();
				}
			});
			jStartFrame.add(jButton2);
		jStartFrame.setVisible(true);
		
		jHelpFrame=new JFrame("帮助");
		jHelpFrame.setBounds(500, 350, 500, 200);
		jHelpFrame.setLayout(new FlowLayout());
			JLabel jHelpLabel=new JLabel();
			jHelpLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
			jHelpLabel.setText("<html><center>游戏规则</center>"
					+ "1.要尊重开发者，不影响使用的bug就不要辣摸多事"
					+ "<br>2.每次输入正确的下落字母分数加10，反之分数减10"
					+ "<br>3.游戏结束的条件为任意一个字母到达底部或点击叉号退出"
					+ "<br>4.最重要的一点就是不要忘记第一点</html>");
			jHelpLabel.setBounds(20, 50, 460, 100);
			jHelpFrame.add(jHelpLabel);
			jHelpFrame.setVisible(false);
		
		
		jFrame=new JFrame("打字游戏");
		jFrame.setBounds(500, 250, 500, 500);
		jFrame.setResizable(false);
		ImageIcon icon=new ImageIcon("morty.jpg");
		JLabel jLabel=new JLabel(icon);
		jLabel.setSize(icon.getIconWidth(), icon.getIconHeight());
		jFrame.getLayeredPane().add(jLabel,new Integer(Integer.MIN_VALUE));
		JPanel jPanel=(JPanel)jFrame.getContentPane();
		jPanel.setOpaque(false);
		jPanel.setLayout(new FlowLayout());
		jFrame.setLayout(null);
		
		//添加游戏结束窗口
		JDialog jOverDialog=new JDialog(jFrame, "游戏结束！");
		jOverDialog.setBounds(500, 400, 500, 150);
		jOverDialog.setResizable(false);								//设置为不可改变窗口大小
		jOverDialog.setLayout(new FlowLayout());						//设置布局
			JLabel jOverLn1=new JLabel("--- GAME OVER ---");
			jOverLn1.setFont(new Font("JetBrains Mono NL", Font.BOLD, 40));
			JButton jBackButton=new JButton("返回主界面");
			jBackButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					jFrame.dispose();
					jStartFrame.setVisible(true);
				}
			});
		jOverDialog.add(jOverLn1);
		jOverDialog.add(jBackButton);
		
		//添加暂停时的窗口
		JDialog jPauseDialog=new JDialog(jFrame, "游戏已暂停");
		jPauseDialog.setBounds(500, 400, 500, 200);
		jPauseDialog.setResizable(false);								//设置为不可改变窗口大小
		jPauseDialog.setLayout(new FlowLayout());						//设置布局
			//添加窗口中的元素
			JLabel jPauseLn1=new JLabel("--- PAUSE ---");
			jPauseLn1.setFont(new Font("JetBrains Mono NL", Font.BOLD, 40));
			JLabel jPauseLn2=new JLabel("游戏已暂停。是否退出？");
			jPauseLn2.setFont(new Font("微软雅黑", Font.BOLD, 16));
			JLabel jPauseLn3=new JLabel("（点击“确认”返回主界面，点击“取消”继续游戏）");
			jPauseLn3.setFont(new Font("微软雅黑", Font.BOLD, 16));
			
			JButton jOKButton=new JButton("确认");
			jOKButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					jStartFrame.setVisible(true);
					jFrame.dispose();
				}
			});
			JButton jCancelButton=new JButton("取消");
			jCancelButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					jPauseDialog.dispose();								//点击后销毁此窗口
					b=true;												//继续进行游戏
				}
			});
			
			JPanel jButtonPanel=new JPanel();
			jButtonPanel.add(jOKButton);
			jButtonPanel.add(jCancelButton);
		jPauseDialog.add(jPauseLn1);
		jPauseDialog.add(jPauseLn2);
		jPauseDialog.add(jPauseLn3);
		jPauseDialog.add(jButtonPanel);

		MyCanvas myCanvas=new MyCanvas(0, 60, colors[c]);				//设置“画布”在窗口中的位置
		myCanvas.setBounds(0, 65, 500, 375);							//设置“画布”坐标及宽高
		myCanvas.setBackground(Color.LIGHT_GRAY);
		myCanvas.addKeyListener(new KeyListener() {						//为“画布”添加键盘事件监听器
			
			@Override
			public void keyTyped(KeyEvent e) {}
			@Override
			public void keyReleased(KeyEvent e) {}
			@Override
			public void keyPressed(KeyEvent e) {
				//按下键时调用
				
				char ch=e.getKeyChar();
				String string=String.valueOf(ch);
				ArrayList<DataInfo> list=myCanvas.getList();
				for (int i = 0; i < list.size(); i++) {
					if (string.equals(list.get(i).getStr())) {
						score=score+10;
						if (score==100) {
							speed=20;
						}else if (score==500) {
							speed=25;
						}else if(score>=1000){
							speed=30;
						}
						list.get(i).setString(strings[random.nextInt(26)]);			//借助Random实现随机字母
						list.get(i).setX(random.nextInt(300));
						myCanvas.setColor(colors[random.nextInt(colors.length)]);
						list.get(i).setY(-10);
						myCanvas.setScore(score);
						myCanvas.setList(list);
						myCanvas.repaint();
					}
					else {
						score=score-10;
					}
				}
				score=score+30;
				myCanvas.setScore(score);
				myCanvas.repaint();
			}
		});
		jFrame.add(myCanvas);
		
		JButton jButtonStart=new JButton("开始");
		jButtonStart.setBounds(10, 5, 100, 45);
		jButtonStart.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Thread thread=new Thread(new Runnable() {
					
					@Override
					public void run() {
						ArrayList<DataInfo> list=myCanvas.getList();
						while (b) {
							for (int i = 0; i < list.size(); i++) {
								try {
									Thread.sleep(100);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
								int y=list.get(i).getY();		//从MyCanvas中获取y值
								y=y+speed;                        //实现加速
								if (y>=335) {
									b=false;
									jOverDialog.setModal(true);
									jOverDialog.setVisible(true);
								}
								list.get(i).setY(y);			//将y值返回给MyCanvas
								myCanvas.setList(list);
								myCanvas.repaint();				//进行重绘
							}
						}
					}
				});
				thread.start();
			}
		});
		jFrame.add(jButtonStart);
		
		//添加“暂停”按钮
		JButton jButtonPause=new JButton("暂停");
		jButtonPause.setBounds(300, 5, 100, 45);
		jButtonPause.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				b=false;
				jPauseDialog.setModal(true);
				jPauseDialog.setVisible(true);
			}
		});
		jFrame.add(jButtonPause);
		
		jFrame.setVisible(false);
	}
}